<template>
  <div class="permission-container">
    <CardHeader style="margin-bottom: 15px">
      <template #left>
        权限设置
      </template>
      <el-button type="primary" @click="addFn(1, '0')">添加权限</el-button>
    </CardHeader>
    <el-card>
      <el-table :data="list" row-key="id">
        <el-table-column label="权限名称" prop="name" />
        <el-table-column label="权限标识" prop="code" />
        <el-table-column label="权限描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" size="small" type="text" @click="addFn(2,row.id)">添加权限</el-button>
            <el-button size="small" type="text" @click="editFn(row)">查看</el-button>
            <el-button size="small" type="text" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :title="showTitle" :visible.sync="showDialog" @close="closeFn">
      <el-form ref="form" label-width="100px" :model="form" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="form.enVisible"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button size="small" @click="closeFn">取消</el-button>
        <el-button size="small" type="primary" @click="addPremission">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { ListToTree } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '1',
        type: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空' }
        ],
        code: [
          { required: true, message: '权限标识不能为空' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '修改权限' : '添加权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const data = await getPermissionList()
      // console.log(data)
      this.list = ListToTree(data, '0')
      // console.log(this.list)
    },
    async delFn(id) {
      try {
        await this.$confirm('你确定要删除该角色吗？', '温馨提示', {
          type: 'warning'
        })
      } catch {
        return
      }
      await delPermission(id)
      this.$message.success('删除权限成功')
      this.getPermissionList()
    },
    addFn(type, pid) {
      this.showDialog = true
      this.form.type = type
      this.form.pid = pid
    },
    async addPremission() {
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      if (this.form.id) {
        await updatePermission(this.form)
        this.$message.success('修改权限成功')
      } else {
        await addPermission(this.form)
        this.$message.success('添加权限成功')
      }
      this.showDialog = false
      this.getPermissionList()
    },
    closeFn() {
      this.showDialog = false
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: '1',
        type: '',
        pid: ''
      }
      this.$refs.form.resetFields()
    },
    async editFn(row) {
      this.showDialog = true
      const data = await getPermissionDetail(row.id)
      // console.log(data)
      this.form = data
    }
  }
}
</script>

<style lang="scss" scoped>
.permission {
  &-container {
    margin: 30px;
  }
}
</style>
